﻿//using ClassLib.DBAuthentication.DataProvider;

using System;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;
using Sumen.Business;
using Sumen.Business.Data;
using Sumen.Business.Presenter.Sys;
using Sumen.GUI;
using Sumen.GUI.Sales;
using Sumen.Library.Classes.Common;
using Sumen.Library.Froms;
using Sumen.Library.Froms.Connection;
using Sumen.Model.Model;

namespace Sumen.Main
{
    public partial class FrmDangNhap : DevExpress.XtraEditors.XtraForm
    {
        Chuoi.EncryptDecrypt ed = new Chuoi.EncryptDecrypt();

        private UserPresenter _userPresenter;
        bool bSucc = false;
        string vPassword = string.Empty;

       Main objFrmMain;
        private string taikhoan = global::Sumen.Main.Properties.Settings.Default.User + string.Empty;
        public FrmDangNhap()
        {
            InitializeComponent();
            _userPresenter = new UserPresenter();
            if (taikhoan == "")
                checkEdit1.CheckState = CheckState.Unchecked;
            else
                checkEdit1.CheckState = CheckState.Checked;
            if (checkEdit1.Checked)
                txtTaiKhoan.Text = taikhoan;
        }

        private void FrmDangNhap_Load(object sender, EventArgs e)
        {
            txtTaiKhoan.Focus();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (KiemTraHopLe() == true)
            {
                this.Enabled = false;
                SplashScreenManager.ShowForm(typeof(WaitForm1));
                user user = _userPresenter.GetById(txtTaiKhoan.Text.Trim());
                if (user != null)
                {
                    vPassword = ClsPublic.EncryptSHA512(txtMatKhau.Text.Trim());
                    if (vPassword == user.User_Password)
                    {
                        if (user.User_Actived == false)
                        {
                            ClsBaoLoi.ThongTin("Tài khoản đã bị khóa.");
                            this.Enabled = true;
                            SplashScreenManager.CloseForm();
                            return;
                        }
                        if (checkEdit1.Checked)
                        {
                            global::Sumen.Main.Properties.Settings.Default.User = txtTaiKhoan.Text.Trim();
                            global::Sumen.Main.Properties.Settings.Default.Save();
                        }
                        else
                        {
                            global::Sumen.Main.Properties.Settings.Default.User = "";
                            global::Sumen.Main.Properties.Settings.Default.Save();
                        }

                        SplashScreenManager.CloseForm();
                        bSucc = true;
                        this.Hide();
                        ClsPublic.User = user;
                        try
                        {
                            Dao dao = new Dao();
                            user.User_LastLoginDate = DateTime.Now;
                            dao.Update(user);
                        }
                        catch
                        {

                        }
                       
                        FormManager formManager = FormManager.getInstance();

                        if (formManager._RibbonForm == null || formManager._RibbonForm.IsDisposed)
                            //objFrmMain = new FrmMain_CTU();
                            formManager._RibbonForm = new global::Sumen.Main.Main();


                        //Application.Run(formManager._RibbonForm);
                        formManager._RibbonForm.Show();
                        formManager._RibbonForm.Activate();

                    }
                    else
                    {
                        ClsBaoLoi.ThongTin("Mật khẩu không hợp lệ.\nBạn vui lòng kiểm tra lại.");
                        vPassword = string.Empty;
                        this.Enabled = true;
                        SplashScreenManager.CloseForm();
                    }
                }
                else
                {
                    ClsBaoLoi.Loi("Tài khoản không tồn tại.\nBạn vui lòng kiểm tra lại.");
                    txtTaiKhoan.Focus();
                    vPassword = string.Empty;
                    this.Enabled = true;
                    SplashScreenManager.CloseForm();
                }

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDangNhap_Click(btnDangNhap, e);
            }
        }

        private void FrmDangNhap_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bSucc == false)
                Application.Exit();
        }

        private bool KiemTraHopLe()
        {
            if (txtTaiKhoan.Text.Trim() == "")
            {
                ClsBaoLoi.ThongTin("Tài khoản không thể rỗng.\nBạn vui lòng nhập tài khoản.");
                txtTaiKhoan.Focus();
                return false;
            }

            if (txtMatKhau.Text.Trim() == "")
            {
                ClsBaoLoi.ThongTin("Mật khẩu không thể rỗng.\nBạn vui lòng nhập mật khẩu.");
                txtMatKhau.Focus();
                return false;
            }

            return true;
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            FrmCauHinhCSDL obj = new FrmCauHinhCSDL();
            obj.Show();
        }


    }
}
